<style lang="scss" rel="stylesheet/scss">
    
</style>
<template>
    <div class="index-detail">
       <router-view></router-view>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        components: {
            
        },
        
        data () {
            return {
                fromType:'',
                detailId:'',
                unionid: ''
            }
        },
        mounted () {
            
        },
        created() {
            // http://192.168.0.114:8001/?id=6939&f=unitary_advert&isapp=true&authinfo=%7B%22key%22%3A%22294b754ef8c6a5b2ec159e523850be89%22%2C%22weixin_yjsb_unionid%22%3A%22oc6KSwzxjQZdzWro6UtjDz10w_Ic%22%7D key,weixin_yjsb_unionid
            let fromParam = location.href.split('#')[0]
            // debugger
            if(fromParam.indexOf('&isapp=true') != -1) {
                localStorage.clear()
                // 从app来
                localStorage.setItem('isapp',true)
                if(fromParam.indexOf('&authinfo') != -1) {
                    // 已经登陆
                    let auth = decodeURIComponent(fromParam.split('authinfo=')[1])
                    auth = JSON.parse(auth)
                    localStorage.setItem('authapp',JSON.stringify(auth))
                }
                this.fetchData()
            }else {
                // 从web端 微信公众号
                localStorage.setItem('isapp',false)
                this.getAuthInfo()
            }
            
            // this.fetchData()
        },
        methods: {
            getAuthInfo() {
                let fromParam = location.href 
                if(fromParam.indexOf('&datas') != -1) { 
                    // 从授权页面返回
                    let datasurl = fromParam.split('&datas=')[1] //&datas=...
                    let data
                    if(datasurl.indexOf('&') != -1) {
                        data = decodeURI(datasurl.split('&')[0])
                    }else {
                        data = decodeURI(datasurl.split('#')[0])
                    }
                    data = JSON.parse(data)
                    data.date = new Date().getTime()
                    localStorage.setItem('auth',JSON.stringify(data))
                    if(data.member_id) {
                        localStorage.setItem('member_id',data.member_id)
                    }
                    // 去掉&datas刷新当前页
                    window.location.replace(location.href.split('#')[0].split('&datas')[0])
                    console.log(JSON.stringify(data))
                    // 获取到授权 去页面
                    // this.fetchData()
                }else {
                    // window.location.href = 'https://wx.yjsb18.xyz/index.php?r=/auth/index'
                    // 判断有没有缓存
                    if(!localStorage.getItem('auth')) {
                        let href = fromParam.split('#')[0]
                        let agentid = 0
                        if(href.indexOf('agent_id=') != -1){
                            agentid = href.split('agent_id=')[1].split('&')[0]
                        }
                        // console.log(`${encodeURIComponent(fromParam.split('#')[0])}`)
                        // debugger
                        window.location.href = `${xmconfig.AUTH_URL}/index.php?r=/auth/index&tophref=${encodeURIComponent(fromParam.split('#')[0])}&agent_id=${agentid}`
                    }else {
                        let localdata = JSON.parse(localStorage.getItem('auth'))
                        let nowtime = new Date().getTime()
                        let a = nowtime - localdata.date
                        if ( (7200 * 1000 + parseInt(localdata.date)) <  nowtime) {
                            // 如果 date + time 小于当前时间的时间戳，说明已经超过37200秒的时效时间
                            let href = fromParam.split('#')[0]
                            let agentid = 0
                            if(href.indexOf('agent_id=') != -1){
                                agentid = href.split('agent_id=')[1].split('&')[0]
                            }
                            window.location.href = `${xmconfig.AUTH_URL}/index.php?r=/auth/index&tophref=${encodeURIComponent(fromParam.split('#')[0])}&agent_id=${agentid}`
                        }
                        console.log(JSON.stringify(localdata))
                        // 获取到授权 去页面
                        this.fetchData()
                    }
                }
                
            },
            getQueryString(name) {
                let reg = new RegExp('(^|&' + name + '=([^&]*)(&|$)','i');
                let r = window.location.search.substr(1).match(reg);
                if(r!=null) {
                    return unescape(r[2])
                }
                return null
            },
            fetchData () {
                let fromParam = location.href // http://192.168.0.103:8001/index?id=6939&f=unitary_advert || &smid=136564&agent_id=890&from=sing....&isappinstalled=0
                if(fromParam.indexOf('&agent_id') != -1) {
                    // alert(fromParam)
                    let datas_agentid = JSON.parse(localStorage.getItem('auth')).agent_id
                    if(datas_agentid) {
                        let agentid_firststr = fromParam.split('&agent_id=')[0]
                        let agentid_laststr = fromParam.split('&agent_id=')[1]
                        let laststr = agentid_laststr.split('&from=')[1]
                        // alert('==agentid_firststr:'+agentid_firststr+'==agentid_laststr:'+agentid_laststr+'==laststr:'+laststr)
                        let newuerl = `${agentid_firststr}&agent_id=${datas_agentid}&from=${laststr}`
                        window.location.replace(newuerl)
                    }
                }
                fromParam = fromParam.split('#')[0]
                if(fromParam.indexOf('&data') != -1) {
                    fromParam = fromParam.split('&data')[0]
                }
                if(fromParam.indexOf('?') != -1) {
                    fromParam = fromParam.split('?')[1]
                    let detail = fromParam.split('&')[0]
                    this.detailId = parseInt(detail.split('=')[1])
                    let from = fromParam.split('&')[1]
                    this.fromType = from.split('=')[1]
                    // 获取位置 并存sessionStorage
                    if(this.fromType === 'unitary_advert') {
                        this.$fetch(`${xmconfig.API_NC}/index.php`,{act:'unitary_advert',op:'adver_getareainfo',id:this.detailId}).then((res) => {
                            let ttext
                            if( !res.datas) {
                                this.$router.replace({name:'error'})
                                return
                            }
                            if(res.datas.adver_areax) {
                                ttext = res.datas.adver_areax
                            }else if(res.datas.adver_areac) {
                                ttext = res.datas.adver_areac
                            }else if(res.datas.adver_areap) {
                                ttext = res.datas.adver_areap
                            }else {
                                ttext = '全国'
                            }
                            sessionStorage.setItem('title',ttext + '信息平台')
                            this.$router.replace({name:'detail',params:{id:this.detailId,fromType:this.fromType}})
                        })
                    }else if(this.fromType === 'local_general') {
                        this.$fetch(`${xmconfig.API_NC}/index.php`,{act:'local_general',op:'local_getareainfo',id:this.detailId}).then((res) => {
                            let ttext
                            if( !res.datas) {
                                this.$router.replace({name:'error'})
                                return
                            }
                            if(res.datas.areax) {
                                ttext = res.datas.areax
                            }else if(res.datas.areac) {
                                ttext = res.datas.areac
                            }else if(res.datas.areap) {
                                ttext = res.datas.areap
                            }else {
                                ttext = '全国'
                            }
                            sessionStorage.setItem('title',ttext + '信息平台')
                            this.$router.replace({name:'detail',params:{id:this.detailId,fromType:this.fromType}})
                        })
                    }else if(this.fromType === 'second_hand') {
                        this.$fetch(`${xmconfig.API_NC}/index.php`,{act:'second_hand',op:'second_getareainfo',id:this.detailId}).then((res) => {
                            let ttext
                            if( !res.datas) {
                                this.$router.replace({name:'error'})
                                return
                            }
                            if(res.datas.areax) {
                                ttext = res.datas.areax
                            }else if(res.datas.areac) {
                                ttext = res.datas.areac
                            }else if(res.datas.areap) {
                                ttext = res.datas.areap
                            }else {
                                ttext = '全国'
                            }
                            sessionStorage.setItem('title',ttext + '信息平台')
                            this.$router.replace({name:'detail',params:{id:this.detailId,fromType:this.fromType}})
                        })
                    }
                    if(fromParam.indexOf('&smid') != -1) {
                        // 如果有member_id
                        let smid = fromParam.split('&')[2]
                        sessionStorage.setItem('smid',smid.split('=')[1])
                    }
                }
            }
        }
    }
</script>

